package com.qf.mvc01.controller;

import com.qf.mvc01.ex.SqlException;
import com.qf.mvc01.ex.UserException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.UUID;

@RestController
@RequestMapping("/up")
public class UploadController {

    @Value("${uploadpath}")
    private String uploadPath;

    @RequestMapping("/test1")
    public String test1(MultipartFile file1,String username) throws IOException {
        //throw new IOException();
        File file = createFile(file1);
        file1.transferTo(file);
        return "upload ok";
    }

    private File createFile(MultipartFile file1) {
        //最终文件名前缀
        String prefix = UUID.randomUUID().toString();

        //原始上传文件的文件名
        String originalFileName = file1.getOriginalFilename();

        //最终文件名
        String fileName = prefix + "_"+originalFileName;


//        Properties p = new Properties();
//        try {
//            p.load(new FileInputStream("E:\\QianFengCode\\Maven\\springMvc\\mvc01\\src\\main\\resources\\fileupload.properties"));
//            String s = p.getProperty("uploadpath");
//            System.out.println(s);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //文件存放路径
        String uploadpath = uploadPath+fileName;

        //
        File file = new File(uploadpath);

        return file;
    }
}
